package com.iafme.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.lang.TypeReference;
import cn.hutool.core.util.StrUtil;
import cn.zhxu.bs.BeanSearcher;
import cn.zhxu.bs.operator.Contain;
import cn.zhxu.bs.util.MapUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.iafme.dto.req.MemberReqDTO;
import com.iafme.dto.res.MemberResDTO;
import com.iafme.model.Member;
import com.iafme.mapper.MemberMapper;
import com.iafme.service.IMemberService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.systemframework.util.SearchUtl;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author summer
 * @since 2025/01/05 21:14:41
 */
@Service
@Transactional(rollbackFor = Exception.class)
@AllArgsConstructor
public class MemberServiceImpl extends ServiceImpl<MemberMapper, Member> implements IMemberService {

   private final BeanSearcher beanSearcher;

    @Override
    public Page<MemberResDTO> lists(MemberReqDTO memberReqDTO) {
        Map<String, Object> build = MapUtils.builder().page(memberReqDTO.getPageNum() - 1, memberReqDTO.getPageSize())
                .field(MemberResDTO::getName, memberReqDTO.getName()).op(Contain.class)
                .field(MemberResDTO::getPhone, memberReqDTO.getPhone()).op(Contain.class)
                .field(MemberResDTO::getIdCard, memberReqDTO.getIdCard()).op(Contain.class)
                .build();
         return SearchUtl.searchDataList(memberReqDTO.getPageNum(),memberReqDTO.getPageSize(),beanSearcher.search(MemberResDTO.class,build));



    }

    @Override
    public MemberResDTO findById(Integer id) {
        return this.baseMapper.findById(id);
    }

    @Override
    public void updateMember(MemberReqDTO memberReqDTO) {
        memberReqDTO.setId(StpUtil.getLoginIdAsInt());
        Member member = Convert.convert(Member.class, memberReqDTO);
        this.updateById(member);
    }

    @Override
    public List<Integer> findMemberIds() {
        return this.list().stream().map(Member::getId).collect(Collectors.toList());
    }
}
